﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml;
using System.Xml.Serialization;
using GlobalRadar.Language;

namespace GlobalRadar.UI
{
    public partial class AppSettings : PhoneApplicationPage
    {
        private IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        //bool beginSettings = true;

        public AppSettings()
        {
            InitializeComponent();
            SetingLanguage();
        }
        private void SetingLanguage()
        {
            if (App.LangVN == true)
            {
                ChangeMapView.Header = ResourceVN.SettingModeView;
                LanguadeSetting.Header = ResourceVN.SettingLanguage;
                caption.Text = ResourceVN.SettingCaption;
                title.Text = ResourceVN.SettingTitle;
                rangerTitle.Text = ResourceVN.SettingRadar;
            }
            else
            {
                ChangeMapView.Header = ResourceEn.SettingModeView;
                LanguadeSetting.Header = ResourceEn.SettingLanguage;
                caption.Text = ResourceEn.SettingCaption;
                title.Text = ResourceEn.SettingTitle;
                rangerTitle.Text = ResourceEn.SettingRadar;
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //beginSettings = true;

            if (settings.Contains("MapView"))
            {
                ChangeMapView.IsChecked= (bool)settings["MapView"];
            }
            if (settings.Contains("LangVN"))
            {
                

                LanguadeSetting.IsChecked = (bool)settings["LangVN"];
            }
            if (settings.Contains("RadarZoom"))
            {
                double value;

                if (double.TryParse(settings["RadarZoom"].ToString(), out value))
                {
                    RadarSlider.Value = Math.Abs(value-9);
                }
            }
            //beginSettings = false;
        }
        private void ChangeMapView_Checked(object sender, RoutedEventArgs e)
        {
            if (settings.Contains("MapView"))
            {
                settings["MapView"] = true;
            }
            else
                settings.Add("MapView", true);
        }

        private void ChangeMapView_Unchecked(object sender, RoutedEventArgs e)
        {
            if (settings.Contains("MapView"))
            {
                settings["MapView"] = false;
            }
            else
                settings.Add("MapView", false);
        }

        private void RadarSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (9 - RadarSlider.Value > 0.5)
            {
                if (settings.Contains("RadarZoom"))
                {
                    settings["RadarZoom"] = 9 - RadarSlider.Value;
                }
                else
                    settings.Add("RadarZoom", 9 - RadarSlider.Value);
            }
            else
                if (settings.Contains("RadarZoom"))
                {
                    settings["RadarZoom"] =0.5;
                }
                else
                    settings.Add("RadarZoom",0.5);
        }

        private void LanguadeSetting_Checked(object sender, RoutedEventArgs e)
        {
            if (settings.Contains("LangVN"))
            {
                settings["LangVN"] = true;
            }
            else
                settings.Add("LangVN", true);

            //if (beginSettings == false)

            //    MessageBox.Show(ResourceVN.ArlertChangerLanguage);
            //else

            //    beginSettings = false;
        }

        private void LanguadeSetting_Unchecked(object sender, RoutedEventArgs e)
        {
            if (settings.Contains("LangVN"))
            {
                settings["LangVN"] = false;
            }
            else
                settings.Add("LangVN", false);

            //if (beginSettings == false)

            //    MessageBox.Show(ResourceEn.ArlertChangerLanguage);
            //else
            //    beginSettings = false;
        }
    }
}